<template>
  <div class="login">
    <div class="container">
      <img src="@/assets/avatar.jpg" alt="" class="avatar" />
      <!-- :model 表单绑定的数据对象 :rules 表单验证规则 label-width表单域标签的宽度-->
      <el-form
        :model="loginForm"
        :rules="loginRules"
        label-width="auto"
        ref="Form"
        class="loginForm"
      >
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-key"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="onLogin"
          >登陆</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { setToken, setUserId } from "../utils/localData";
import { userLoginAPI } from "../api/user";
export default {
  data() {
    return {
      loginForm: {
        username: "10000000000",
        password: "123",
      },
      loginRules: {
        //表单的验证规则
        username: [
          //required:true 必填  message提示信息   trigger:blur 触发时机,blur失去焦点时
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            //本地的只有10086的账号才可以发布文章
            pattern: /(^1[01235789]\d{9}$|10086|10010)/,
            message: "请输入正确的手机号",
            trigger: "blur",
          }, // pattern:指定正则规则
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 18, message: "密码必须是3到18位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    onLogin() {
      this.$refs.Form.validate(async (valid) => {
        if (valid) {
          //调用登录api
          const res = await userLoginAPI(this.loginForm);
          console.log(res);
          if (res.data.message === "登录成功") {
            //保存token id
            setToken(res.data.data.token);
            setUserId(res.data.data.user.id);
            // 成功提示
            this.$message.success(res.data.message);
            this.$router.push("/home");
          } else {
            this.$message.error("用户名或密码错误");
          }
        } else {
          this.$message.error("请确认信息是否全正确了");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>